﻿using Newtonsoft.Json;
using WoodAdmin.Common.Helper.Net;
using WoodAdmin.Models;
using System.Collections.Generic;

namespace WoodAdmin.Common.Helper
{
    public class GlobalHelper
    {
        public const string CurrentUserInfo = "CurrentUserInfo";
        public const string CurrentUserAuthNode = "CurrentUserAuthNode";
        public const string CurrentUserRoles = "CurrentUserRole";
        public const string VerifyCode = "VerifyCode";


        /// <summary>
        /// 写入session
        /// </summary>
        /// <param name="sysUser">用户登录信息</param>
        public static void SetCurrentUserInfo(SysUser sysUser)
        {
            HttpContextHelper.SetSession(CurrentUserInfo, sysUser);
        }

        /// <summary>
        /// 设置当前登录用户的角色
        /// </summary>
        /// <param name="list"></param>
        public static void SetCurrentUserRole(List<int> list)
        {
            HttpContextHelper.SetSession(CurrentUserRoles, list);
        }

        /// <summary>
        /// 设置当前登录用户的权限节点
        /// </summary>
        /// <param name="list"></param>
        public static void SetCurrentAuthNode(List<SysNode> list)
        {
            HttpContextHelper.SetSession(CurrentUserAuthNode, list);
        }

        /// <summary>
        /// 获取当前登录用户信息
        /// </summary>
        /// <returns></returns>
        public static SysUser CurrentUser()
        {
            var userStr = HttpContextHelper.GetSession(CurrentUserInfo);
            if (!string.IsNullOrEmpty(userStr))
            {
                return JsonConvert.DeserializeObject<SysUser>(userStr);
            }
            return null;
        }

        /// <summary>
        /// 获取当前登录用户的权限节点
        /// </summary>
        /// <returns></returns>
        public static List<SysNode> CurrentAuthNode()
        {
            var userStr = HttpContextHelper.GetSession(CurrentUserAuthNode);
            if (!string.IsNullOrEmpty(userStr))
            {
                return JsonConvert.DeserializeObject<List<SysNode>>(userStr);
            }
            return null;
        }

        /// <summary>
        /// 获取当前登录用户的角色ID
        /// </summary>
        /// <returns></returns>
        public static List<int> CurrentUserRole()
        {
            var userStr = HttpContextHelper.GetSession(CurrentUserRoles);
            if (!string.IsNullOrEmpty(userStr))
            {
                return JsonConvert.DeserializeObject<List<int>>(userStr);
            }
            return null;
        }

        /// <summary>
        /// 获取当前验证码
        /// </summary>
        /// <returns></returns>
        public static string CurrentVerify()
        {
            return HttpContextHelper.GetSession(VerifyCode);
        }

        /// <summary>
        /// 设置当前验证码
        /// </summary>
        /// <param name="code"></param>
        public static void SetVerify(string code)
        {
            HttpContextHelper.SetSession(VerifyCode, code);
        }

        /// <summary>
        /// 退出登录
        /// </summary>
        public static void SignOut()
        {
            HttpContextHelper.SetSession(CurrentUserInfo, string.Empty);
            HttpContextHelper.SetSession(CurrentUserAuthNode, string.Empty);
        }
    }
}
